﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1.Filtry
{
    class Bernsen
    {
        public Bitmap bitmapFiltr { get; set; }
        private int MacierzWidthIHight,width,height;
        private Bitmapp bitmap; 

        public Bernsen(int MacierzWidthIHight, int width, int height, Bitmapp bitmap, double K)
        {
            this.width = width; this.height = height;
            this.bitmap = bitmap; this.MacierzWidthIHight = MacierzWidthIHight;
        }

        internal System.Drawing.Bitmap ZwrocObrazekBernsen()
        {
            this.bitmapFiltr = new Bitmap(this.width, this.height);
            Double MaxR = 0, MaxG = 0, MaxB = 0;
            Double MinR = 0, MinG = 0, MinB = 0,Min,Max,Prog ;
            int xPixel , yPixel;

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    if (j <= MacierzWidthIHight / 2 || j > this.width - MacierzWidthIHight / 2 - 1 || i <= MacierzWidthIHight / 2 || i > this.height - MacierzWidthIHight / 2 - 1)
                    {
                        bitmapFiltr.SetPixel(j, i, Color.FromArgb(255,255,255));
                    }
                    else
                    {
                        xPixel = j - MacierzWidthIHight / 2; yPixel = i - MacierzWidthIHight / 2;
                        MaxR = 0; MaxG = 0; MaxB = 0;
                        MinR = 255; MinG = 255; MinB = 255;
                        for (int ii = 0; ii < MacierzWidthIHight; ii++)
                        {
                            for (int jj = 0; jj < MacierzWidthIHight; jj++)
                            {
                                if(Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel))<MinR) MinR=Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel));
                                 if(Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel))<MinG) MinG=Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel));
                                 if(Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel))<MinB) MinB=Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel));
                                
                                if(Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel))>MaxR) MaxR=Convert.ToDouble(bitmap.GetPixelR(xPixel, yPixel));
                                 if(Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel))>MaxG) MaxG=Convert.ToDouble(bitmap.GetPixelG(xPixel, yPixel));
                                 if(Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel))>MaxB) MaxB=Convert.ToDouble(bitmap.GetPixelB(xPixel, yPixel));
                                
                                xPixel++;
                            }
                            yPixel++; xPixel = j - MacierzWidthIHight / 2;
                        }
                        Min = (MinR + MinG + MinB) / 3;
                        Max = (MaxR + MaxG + MaxB) / 3;
                        Prog=(Min+Max)/2;

                        if (bitmap.GetPixelR(j, i) < Prog || bitmap.GetPixelG(j, i) < Prog || bitmap.GetPixelB(j, i) < Prog)
                            bitmapFiltr.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                        else bitmapFiltr.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                    }
                }
            }

            return this.bitmapFiltr;
        }
    }
}
